package org.hxyjs.service.impl;


import org.hxyjs.dao.NewsDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.News;
import org.hxyjs.entity.Parents;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.NewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("NewsService")
public class NewsServiceImpl implements NewsService {
    @Resource
    NewsDao newsDao;
    @Override
    public DTO<News> getAllNewss()  throws Exception{
        List<News> newss = newsDao.getAllNewss();
        MessageEnum messageEnum  = Optional.ofNullable(newss).isPresent()?MessageEnum.GETNEWSSUCCESS: MessageEnum.GETNEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage(),newss);
    }

    @Override
    public DTO<News> deleteNewsByNno(String nno)  throws Exception{
        Integer result = newsDao.deleteNewsByNno(nno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETENEWSSUCCESS:MessageEnum.DELETENEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<News> updateNewsByNno(News news)  throws Exception{
        Integer result = newsDao.updateNewsByNno(news);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATENEWSSUCCESS:MessageEnum.UPDATENEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<News> addNews(News news) throws Exception {
        Integer result = newsDao.addNews(news);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDNEWSSUCCESS:MessageEnum.ADDNEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<News> getNewsByPage(PageInfo pageInfo) throws Exception{
        List<News> newss = newsDao.getNewsByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(newss).isPresent()?MessageEnum.GETNEWSSUCCESS: MessageEnum.GETNEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage(),newss,pageInfo);
    }

    @Override
    public DTO<News> selectNewsWithCarouselmap(String nno) throws Exception{
        News newss = newsDao.selectNewsWithCarouselmap(nno);
        MessageEnum messageEnum  = Optional.ofNullable(nno).isPresent()?MessageEnum.GETNEWSSUCCESS: MessageEnum.GETNEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage(),newss);
    }
}
